package com.pine.common.beans.response;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serial;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 分页bean
 * </p>
 *
 * @author pine manage
 * @since 2024-08-09
 */
@Data
@Schema(title = "PageResponse", description = "分页响应")
public class PageResponse<T> implements Serializable {
    @Serial
    private static final long serialVersionUID = 6663045409669488379L;

    /**
     * 数据列表
     */
    @JsonProperty("data")
    @Schema(title = "data", description = "数据列表")
    private List<T> data;

    /**
     * 总数据行数
     */
    @JsonProperty("total")
    @Schema(title = "total", description = "总数据行数", defaultValue = "0")
    private Integer total;

    /**
     * 页数
     */
    @JsonProperty("pages")
    @Schema(title = "pages", description = "页数", defaultValue = "0")
    private Integer pages;

    /**
     * 每页大小
     */
    @JsonProperty("pageSize")
    @Schema(title = "pageSize", description = "每页大小", defaultValue = "0")
    private Integer pageSize;

    /**
     * 当前页
     */
    @JsonProperty("pageNum")
    @Schema(title = "pageNum", description = "当前页", defaultValue = "1")
    private Integer pageNum;

    public PageResponse() {
        this.data = new ArrayList<>(0);
        this.total = 0;
        this.pages = 0;
        this.pageNum = 1;
        this.pageSize = 0;
    }

    public PageResponse(List<T> data, Integer total, Integer page, Integer pageSize) {
        this.data = (null == data ? new ArrayList<T>(0) : data);
        this.total = total;
        this.pageNum = page;
        this.pageSize = pageSize;
        this.pages = (int) Math.ceil((double) total / (double) pageSize);
    }

    public PageResponse(Integer total, Integer pages, Integer pageSize, Integer pageNum) {
        this.total = total;
        this.pages = pages;
        this.pageSize = pageSize;
        this.pageNum = pageNum;
    }
}
